package com.zs.demo.shiro;


import com.zs.demo.common.util.EncryptUtil;
import com.zs.demo.shiro.jwt.JwtUtil;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;

/**
 * @author licoy.cn
 * @version 2017/9/25
 */
public class MyCredentialsMatcher extends SimpleCredentialsMatcher {

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        MyToken myToken = (MyToken) token;
        Object accountCredentials = getCredentials(info);
        if(myToken.getPassword()!=null){
            Object tokenCredentials = EncryptUtil.md5(String.valueOf(myToken.getPassword())+ myToken.getUsername());
            if(!accountCredentials.equals(tokenCredentials)){
                throw new DisabledAccountException("密码不正确！");
            }
        }else{
            boolean verify = JwtUtil.verifyToken(myToken.getToken(), myToken.getUsername(), accountCredentials.toString());
            if(!verify){
                throw new DisabledAccountException("verifyFail");
            }
        }
        return true;
    }
}
